package com.eeepay.eeepay_v2.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.activity.DataDetailsActivity;
import com.eeepay.eeepay_v2.adapter.RateQueryAdapter;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import cn.eeepay.api.grpc.nano.MerAddModular;
import cn.eeepay.api.grpc.nano.MerAddServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * Created by Ching on 2016/6/29.
 */
public class RateFragment extends ABBaseFragment {

    private static RateFragment rateFragment;
    private View view;
    private ListView rateLv;

    private RateQueryAdapter adapter;
    private MerAddModular.MerServiceRate[] rateList;
//    private String bpId, merNo;//业务产品和商户ID

//    public static RateFragment getInstance(MerAddModular.MerServiceRate[] rateList) {
//        if (rateFragment == null) {
//            synchronized (RateFragment.class) {
//                if (rateFragment == null) {
//                    rateFragment = new RateFragment(rateList);
//                }
//            }
//        }
//        return rateFragment;
//    }

    public RateFragment() {
    }

//    public RateFragment(String bpId, String merNo) {
//        this.bpId = bpId;
//        this.merNo = merNo;
//    }

//    public RateFragment(MerAddModular.MerServiceRate[] rateList) {
//        this.rateList = rateList;
//    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_rate, container, false);
        initView();
        initLinstener();
        initData();
        return view;
    }

    private void initView() {
        rateLv = (ListView) view.findViewById(R.id.lv_intoQuery_rate);
    }

    private void initLinstener() {
    }

    private void initData() {
        rateList = ((DataDetailsActivity)getActivity()).getRateList();
        adapter = new RateQueryAdapter(getActivity());
        if (rateList != null) {
            adapter.addAll(rateList);
            rateLv.setAdapter(adapter);
        }
//        if (rateList == null || rateList.length == 0)
//        sendHttpRequest(0);
//        datas = new MerAddModular.ServiceRate[4];
//        MerAddModular.ServiceRate rate;
//        for (int i = 0; i < 4; i++) {
//            rate = new MerAddModular.ServiceRate();
//            rate.cardType = String.valueOf(i);
//            rate.holidaysMark = String.valueOf(i);
//            rate.rateType = String.valueOf(i + 1);
//            switch (Integer.parseInt(rate.rateType)) {
//                case 1:
//                    rate.singleNumAmount = "1";
//                    break;
//                case 2:
//                    rate.rate = "2";
//                    break;
//                case 3:
//                    rate.safeLine = "0";
//                    rate.rate = "0.38";
//                    rate.capping = "3";
//                    break;
//                case 4:
//                    rate.rate = "0.40";
//                    rate.singleNumAmount = "4";
//                    break;
//            }
//            rate.serviceName = "提现";
//            rate.serviceId = String.valueOf(i);
//            datas[i] = rate;
//        }
//
//        adapter.addAll(datas);
//        rateLv.setAdapter(adapter);
    }

//    @Override
//    protected void sendHttpRequest(int taskId) {
//        showProgressDialog("加载中，请稍等");
//        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT
//                , taskId, new GrpcUtils.AsyncTaskListener() {
//                    @Override
//                    public Object doInBack(ManagedChannel channel, int taskId) {
//                        MerAddServiceGrpc.MerAddServiceBlockingStub stub = MerAddServiceGrpc.newBlockingStub(channel);
//                        MerAddModular.QueryMerInfoReq queryReq = new MerAddModular.QueryMerInfoReq();
//                        UserInfo userInfo = UserInfo.getInstance();
//
//                        queryReq.oneAgentNo = userInfo.getOneAgentNo();
//                        queryReq.parentNode = userInfo.getAgentNode();
//                        Log.d("saki", " bpId " + bpId + " || merchantNo" + merNo);
//                        queryReq.bpId = bpId;
//                        queryReq.merchantNo = merNo;
//                        return stub.queryMerSerRate(queryReq);
//                    }
//
//                    @Override
//                    public void updateUI(Object result, int taskId) {
//                        dismissProgressDialog();
//                        if (result == null) {
//                            showToast("获取数据失败");
//                        }
//                        MerAddModular.MerServiceRateList response = (MerAddModular.MerServiceRateList) result;
//                        if (response.resMsg.status) {
//                            rateList = response.merServiceRateList;
//                            MerAddModular.MerServiceRate rate;
//                            for (int i = 0; i < rateList.length; i++) {
//                                rate = rateList[i];
//                                Log.d("qRate", "cardType:" + rate.cardType + "|-|  cardType:" + rate.rate);
//                            }
//                            adapter.addAll(rateList);
//                            rateLv.setAdapter(adapter);
//                        } else {
//                            showToast(response.resMsg.msg);
//                        }
//                    }
//                });
//    }

//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        if (isVisibleToUser) {//相当于Fragment的onResume
//            if (rateList == null || rateList.length == 0)
//                sendHttpRequest(0);
//        } else { //相当于Fragment的onPause
//
//        }
//    }
}
